package savi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import savi.classes.Accounts;

@SuppressWarnings("serial")
public class SetupServlet extends HttpServlet {
	private HttpServletRequest request;
	private HttpServletResponse response;

	// OPERATIONS
	public static final String USER_LOGIN = "user_login";
	public static final String USER_LOGOUT = "user_logout";
	public static final String TO_REGISTER = "to_register";
	public static final String GET_GOOGLE_ID = "get_google_id";
	public static final String USER_GET_MODULE_LIST = "user_get_module_list";
	public static final String ADD_USER_MODULE = "user_add_module";
	public static final String DELETE_USER_MODULE = "user_delete_module";
	public static final String USER_ADD_MODULE_LIST = "user_add_module_list";
	
	// PARAMETERS
	public static final String MODULE_ID = "moduleID";

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		this.request = req;
		this.response = resp;

		String operation = request.getParameter("operation");

		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");

		if (operation.equals(USER_LOGIN)) {
			userLogin();
		} else if (operation.equals(USER_LOGOUT)) {
			userLogout();
		} else if (operation.equals(ADD_USER_MODULE)) {
			userAddModule();
		} else if (operation.equals(USER_ADD_MODULE_LIST)) {
			userAddModuleList();
		} else if (operation.equals(TO_REGISTER)) {
			toRegister();
		} else if (operation.equals(USER_GET_MODULE_LIST))
			getUserModuleList();
		else if(operation.equals(DELETE_USER_MODULE)){
			userDeleteModule();  
		}
		else {
			System.out.println("Unknown command\n");
		}
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		this.request = req;
		this.response = resp;

		String operation = request.getParameter("operation");

		if (operation.equals(GET_GOOGLE_ID)) {
			getGoogleId();
		} else {
			System.out.println("Unknown command\n");
		}
	}

	private void userLogin() throws IOException {
		Accounts accounts = Accounts.inst();
		response.getWriter().write(
				"<url><![CDATA[" + accounts.getLoginPage("/home.jsp")
						+ "]]></url>");
	}

	private void userLogout() throws IOException {
		Accounts accounts = Accounts.inst();
		response.getWriter().write(
				"<url><![CDATA[" + accounts.getLogoutPage("/index.jsp")
						+ "]]></url>");
	}

	private void toRegister() throws IOException {
		Accounts accounts = Accounts.inst();
		response.setContentType("text/plain");
		response.getWriter().write(
				Boolean.toString(accounts.modulesRetrieved()));
	}

	private void getGoogleId() throws IOException {
		Accounts accounts = Accounts.inst();
		response.setContentType("text/plain");
		response.getWriter().write(accounts.getUserName());
	}

	public void userAddModule() throws IOException {
		Accounts accounts = Accounts.inst();
		String moduleID = request.getParameter(MODULE_ID);
		accounts.addUserModule(moduleID);
	}

	public void userDeleteModule() throws IOException {
		Accounts accounts = Accounts.inst();
		String moduleID = request.getParameter(MODULE_ID);
		accounts.deleteUserModule(moduleID);
	}

	private void userAddModuleList() throws IOException {
		Accounts accounts = Accounts.inst();
		List<String> moduleList = new ArrayList<String>();
		int modulesLength = Integer.parseInt(request.getParameter("length"));
		for (int i = 0; i < modulesLength; i++)
			moduleList.add(request.getParameter("module" + i));
		accounts.addUserModuleList(moduleList);
	}

	private void getUserModuleList() throws IOException {
		Accounts accounts = Accounts.inst();
		String result = accounts.getUserModuleList();
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.close();
	}
}
